package zsc.ruanc.practicaltraining.model.domain;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.validator.constraints.NotBlank;
import zsc.ruanc.practicaltraining.model.vo.ArticleVo;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Date;

/**
 * 文章表
 */
@AllArgsConstructor
@NoArgsConstructor
@Data
public class TArticle {

  @NotNull(message = "id不能为空")
  @Min(message = "id不能为小于0", value = 0)
  @ApiModelProperty(value = "文章id，自增主键", required = true, dataType = "int")
  private Integer id;

  @NotBlank(message = "title不能为空")
  @ApiModelProperty(value = "文章标题", required = true, dataType = "string")
  private String title;

  @NotBlank(message = "content不能为空")
  @ApiModelProperty(value = "文章内容", required = true, dataType = "string")
  private String content;

  @ApiModelProperty(value = "文章创建时间", required = false, dataType = "Date")
  private Date created;

  @ApiModelProperty(value = "文章修改时间", required = false, dataType = "Date")
  private Date modified;

  @ApiModelProperty(value = "文章分类", required = false, dataType = "string")
  private String categories;

  @ApiModelProperty(value = "文章标签", required = false, dataType = "string")
  private String tags;

  @ApiModelProperty(value = "是否允许评论", required = false, dataType = "bool")
  private Boolean allowComment;

  @ApiModelProperty(value = "文章是否通过审核", required = false, dataType = "bool")
  private Boolean flag;

  @ApiModelProperty(value = "作者id", required = false, dataType = "int")
  private int userId;

  @ApiModelProperty(value = "减少分页查找传送量", required = false, dataType = "String")
  private String smallText;

  @ApiModelProperty(value = "是否能被程序自动审核", required = false, dataType = "bool")
  private Boolean autoFlag;


  public TArticle(ArticleVo vo) {

    this.title = vo.getTitle();
    this.content = vo.getContent();
    this.categories = vo.getCategories();
    this.tags = vo.getTags();
    if (vo.getAllowComment() == null) {
      this.allowComment = true;
    } else {
      this.allowComment = vo.getAllowComment();
    }

    this.created = new Date();
    this.modified = this.created;

    if (vo.getSmallText() == null) {
      if (this.content.length() <= 50) {
        this.smallText = this.content;
      } else {
        this.smallText = this.content.substring(50);
      }
    } else {
      this.smallText = vo.getSmallText();
    }

  }

}
